﻿using System;
using System.Xml;
using StarTrek.Ship;
using StarTrek.World;

namespace StarTrek.Space
{
    class Planet:iSpaceItem
    {
        private char Classification;
        private string Name;
        private float Mass;
        private float EscapeVel;

        private iShip vessel;

        private Quadrant parent;
        private Guid id;
        private iShip PresentVessel = null;

        public Planet(char PlanetaryClassification):this()
        {
            Classification = PlanetaryClassification;
        }
        public Planet()
        {
            id = Guid.NewGuid();
        }

        public override string ToString()
        {
            string format = "{0} class Planet, {1}";
            return string.Format(format, Classification, Name);
        }
        #region iSpaceItem Members

        public Quadrant Parent
        {
            get
            {
                return parent;
            }
            set
            {
                parent = value;
            }
        }

        public bool isShipDocked()
        {
            return (vessel != null);
        }

        public iShip DockedShip
        {
            get
            {
                return vessel;
            }
            set
            {
                vessel = value;
            }
        }
        public iShip PresentShip
        {
            get
            {
                return PresentVessel;
            }
            set
            {
                PresentVessel = value;
            }
        }
        public SpaceUnitInfo BuildInfo()
        {
            throw new NotImplementedException();
        }

        public float CalculateDistance(iSpaceItem Target)
        {
            if (parent.Contains(Target))
                return parent.CalculateDistance(this, Target);
            else
                return Parent.Parent.CalculateDistance(this, Target);
        }

        public float TerrainPenalty
        {
            get { return EscapeVel; }
        }

        public Guid SectorIdentifier
        {
            get { return id; }
        }
        public iSpaceItem FromXml(XmlNode val)
        {
            foreach (XmlNode value in val.ChildNodes)
            {
                switch (value.Name.ToUpper())
                {
                    case "CLASSIFICATION":
                        this.Classification = char.ToUpper(value.InnerText.ToCharArray()[0]);
                        break;
                    case "NAME":
                        this.Name= value.InnerText;
                        break;
                    case "MASS":
                        float.TryParse(value.InnerText, out this.Mass);
                        break;
                    case "ESCAPEVEL":
                        float.TryParse(value.InnerText, out this.EscapeVel);
                        break;
                    case "#COMMENT":
                        break;
                    default:
                        throw new NotImplementedException();
                }
            }
            return this;
        }
        public bool isDockable
        {
            get { return true; }
        }

        public bool isTravelable
        {
            get { return true; }
        }
        #endregion
    }
}
